<template>
  <div>
    <div class="back">
      <van-icon
        name="arrow-left"
        color="#fff"
        size="20px"
        style="margin: 10px"
        @click="tap(index)"
      />
    </div>
    <div class="top">
      <div class="background">
        <img :src="cover" />
      </div>
      <div class="introduce">
        <div class="name">
          {{ name }}
        </div>
        <div class="nickname">
          <span>昵称：{{ nickname }}</span>
          <img
            class="vip_icon"
            src=""
          />
          <img
            class="package_icon"
            src=""
          />
        </div>
        <div class="attention">
          <span>关注 {{ followCnt }}</span>
          <div class="vertical_bar"></div>
          <span>粉丝</span>
          <div class="fansCnt">{{ formatNumber(fansCnt) }}</div>
        </div>
        <div class="musicion">
          <span>{{ identifyTag }}</span>
          <div
            class="identities"
            :class="{ actived: currentIndex == index }"
            v-for="(identities, index) in ident"
            :key="index"
          >
            {{ identities }}
          </div>
        </div>
      </div>
    </div>

    <div class="centre">
      <div class="title">歌手简介</div>
      <div
        class="detail"
        v-if="isTextCollapsed"
        :style="{
          overflow: 'hidden',
          display: '-webkit-box',
          '-webkit-line-clamp': '3',
          '-webkit-box-orient': 'vertical',
        }"
      >
        {{ briefDesc }}
      </div>
      <div class="detail" v-else>
        {{ briefDesc }}
      </div>
      <button @click="toggleText">
        {{ isTextCollapsed ? "展开" : "收起" }}
        <div class="arrow" v-if="isTextCollapsed">
          <van-icon name="arrow-down" />
        </div>
        <div class="arrow" v-else>
          <van-icon name="arrow-up" />
        </div>
      </button>
    </div>

    <div class="space"></div>
    <!--加载中...-->
    <GlobalLoading v-if="isShowLoading"></GlobalLoading>
    <div class="bottom">
      <div class="title">热门单曲</div>
      <div class="music">
        <div class="left">
          <div
            class="number"
            :class="{ actived: currentIndex == index }"
            v-for="(item, index) in blockList"
            :key="index.Id"
          >
            <div class="order">{{ index + 1 }}</div>
          </div>
        </div>
        <div class="right">
          <GlobalSongs :blockList="blockList"></GlobalSongs>
        </div>
      </div>
    </div>
  </div>
</template>
      
  <script setup>
import GlobalSongs from "@/components/GlobalSongs.vue";
import GlobalLoading from "@/components/GlobalLoading.vue";
import { reactive, onMounted, ref } from "vue";
import { showToast } from "vant";
import { useMainStore } from "@/pinia/store.ts";
import router from "@/router/routes";
import { useRoute } from "vue-router";
import axios from "axios";
import {
  getSingerDetailsApi,
  getSingerSongsApi,
  getSingerCountsApi,
} from "@/request/api/home.ts";

const currentIndex = ref(0);
const mainStore = useMainStore();
const route = useRoute();
const isShowLoading = ref([]);
const value = ref("");

// detail
const cover = ref();
const name = ref();
const nickname = ref();
const followCnt = ref();
const fansCnt = ref();
// 将大于一万的数字转换为小数形式
const formatNumber = (num) => {
  if (num > 10000) {
    return (num / 10000).toFixed(2) + " 万";
  }
  return num;
};
const briefDesc = ref();
const isTextCollapsed = ref(true);
// 展开、收起
const toggleText = () => {
  isTextCollapsed.value = !isTextCollapsed.value;
};
// song
const blockList = ref([]);
// count
const identifyTag = ref();
const ident = ref([]);

const tap = (index) => {
  console.log();
  router.go(-1);
};

// 获取首页编辑推荐中的id
function getQueryStringParams(url = window.location.href) {
  let queryParams = {};
  // 使用URLSearchParams解析当前页面URL中的查询字符串参数
  let urlParams = new URLSearchParams(url.split("?")[1]);
  for (const [key, value] of urlParams.entries()) {
    queryParams[key] = value;
  }
  return queryParams;
}
let id = getQueryStringParams();
console.log(id);

const getSingerDetails = async () => {
  const res = await getSingerDetailsApi(id);
  console.log(res.data);
  //歌手信息
  cover.value = res.data.data.artist.cover;
  name.value = res.data.data.artist.name;
  nickname.value = res.data.data.user.nickname;
  identifyTag.value = res.data.data.artist.identifyTag[0];
  ident.value = res.data.data.artist.identities;
  briefDesc.value = res.data.data.artist?.briefDesc;
};
const getSingerSongs = async () => {
  const res = await getSingerSongsApi(id);
  console.log(res.data);
  blockList.value = res.data.songs;
};
const getSingerCounts = async () => {
  const res = await getSingerCountsApi(id);
  console.log(res.data);
  followCnt.value = res.data.data.followCnt;
  fansCnt.value = res.data.data.fansCnt;
};
onMounted(async () => {
  await getSingerDetails();
  await getSingerSongs();
  await getSingerCounts();
  isShowLoading.value = false;
});
</script>
      
<style lang="scss" scoped>
.back {
  position: fixed;
}

.top {
  display: block;
  .background {
    background-color: #666;
    position: relative;
    z-index: -1;
    overflow: hidden;
    width: 375px;
    height: 300px;
    img {
      display: block;
      width: 100%;
      height: 100%;
    }
  }
  .introduce {
    position: absolute;
    top: 180px;
    padding: 0 10px;
    color: #fff;
    display: block;
    align-items: center;
    margin-bottom: 20px;
    .name {
      display: flex;
      flex-wrap: wrap;
      margin: 1px 0 0 0;
      font-size: 20px;
      font-weight: 700;
    }
    .nickname {
      display: flex;
      flex-wrap: wrap;
      margin: 5px 0 0 0;
      span {
        font-size: 13px;
      }
      .vip_icon {
        width: 22px;
        height: 12px;
        margin: 4px 0 0 6px;
      }
      .package_icon {
        width: 12px;
        height: 12px;
        margin: 4px 0 0 6px;
      }
    }
    .attention {
      display: flex;
      flex-wrap: wrap;
      margin: 4px 0 0 0;
      span {
        font-size: 13px;
      }
      .vertical_bar {
        width: 1px;
        height: 10px;
        background-color: #fff;
        margin: 4px 10px 0 10px;
      }
      .fansCnt {
        font-size: 13px;
        margin: 2px 0 0 6px;
      }
    }
    .musicion {
      display: flex;
      flex-wrap: wrap;
      margin: 5px 0 0 0;
      span {
        font-size: 13px;
        margin-right: 4px;
      }
      .identities {
        border: 1px solid #fff;
        border-radius: 4px;
        display: inline-block;
        font-size: 10px;
        margin: 1px 4px 1px 0;
        padding: 0 2px 1px 2px;
      }
    }
  }
}

.centre {
  display: block;
  .title {
    font-size: 20px;
    color: #000;
    font-weight: 700;
    margin: 10px;
    padding-top: 1px;
  }
  .detail {
    font-size: 13px;
    color: #505050;
    margin: 13px 10px 0 10px;
    overflow: hidden;
    line-height: 1.5;
  }
  button {
    position: relative;
    bottom: 0;
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    justify-content: center;
    align-items: center;
    background: none;
    border: none;
    color: #666;
    font-size: 14px;
    cursor: pointer;
    margin: 15px 0 12px 0;
  }
}

.space {
  height: 10px;
  background-color: #eee;
  margin-bottom: 10px;
}

.bottom {
  display: block;
  .title {
    font-size: 20px;
    color: #000;
    font-weight: 700;
    margin: 21px 10px 15px 10px;
  }
  .music {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 100%;
    .left {
      display: block;
      width: 10%;
      height: 100%;
      .number {
        .order {
          display: flex;
          height: 56px;
          justify-content: center;
          align-items: center;
          color: #999;
          font-size: 17px;
        }
      }
    }
    .right {
      display: block;
      width: 90%;
      height: 100%;
      padding-right: 10px;
    }
  }
}
</style>